#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <SDL/SDL.h>

int main ( int argc, char** argv )
{   int x,y;

freopen("CON","w",stdout);
freopen("CON","r",stdin);

    // initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    // make sure SDL cleans up before exit
    atexit(SDL_Quit);

    // create a new window
    SDL_Surface* screen = SDL_SetVideoMode(1280, 720, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    // load an image
   // SDL_Surface* merci = SDL_LoadBMP("merci.bmp");
    SDL_Surface* game_menu = SDL_LoadBMP("Game_menu.bmp");
   //SDL_Surface* ske_head= SDL_LoadBMP("ske_head.bmp");
   //SDL_Surface* bmp;
   // if (!bmp)
   // {
   //     printf("Unable to load bitmap: %s\n", SDL_GetError());
   //     return 1;
   // }

    // centre the bitmap on screen
   // SDL_Rect dstrect4;
   // dstrect4.x = (screen->w - bmp  ->w) / 2;
    //dstrect4.y = (screen->h - bmp->h) / 2;
    SDL_Rect dstrect,dest1,dest2,dest3;
    dstrect.x = (screen->w - game_menu->w) / 2;
    dstrect.y = (screen->h - game_menu->h) / 2;
    //SDL_Rect ds2;
   // ds2.x = (screen->w - ske_head->w) / 2;
   // ds2.y = (screen->h - ske_head->h) / 2;

    // program main loop
    bool done = false;
    while (!done)
    {  SDL_BlitSurface(game_menu, 0, screen, &dstrect);
        // message processing loop
        SDL_Event event;
        while (SDL_PollEvent(&event))
            switch (event.type)
        {  case SDL_MOUSEMOTION:
            {int x=event.motion.x;
             int y=event.motion.y;
            printf("%d\n",x);
            printf("%d\n",y);
            break;
            }
             int choix ;
              case SDL_MOUSEBUTTONDOWN:
            {int x=event.motion.x;
            int y=event.motion.y;
            if(x>=321&&x<=954&&y<=351&&y>=295)
           {   choix=1;
                  SDL_FreeSurface(game_menu);
         SDL_Surface* screen = SDL_SetVideoMode(1280,720, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
           //  SDL_Surface* bmp =  SDL_LoadBMP("index.bmp");
            //  SDL_Rect dstrect4;
    //dstrect4.x = (screen->w - bmp  ->w) / 2;
    //dstrect4.y = (screen->h - bmp->h) / 2;
             //SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 0));

               SDL_Surface* ske_head= SDL_LoadBMP("ske_head.bmp");

                SDL_Rect ds2;
                ds2.x = (screen->w - ske_head->w) / 2;
                ds2.y = (screen->h - ske_head->h) / 2;
//SDL_BlitSurface(bmp, 0, screen, &dstrect4);
                 SDL_BlitSurface(ske_head, 0, screen, &ds2);


             while(choix=1) {

               if(event.key.keysym.sym == SDLK_RIGHT)
               {
                    if((ds2.y + ske_head->w) < (screen->w))
                       ds2.y+=10;

               }
             if(event.key.keysym.sym == SDLK_LEFT)
                {
                    if((ds2.x) > 0) //limite gauche
                       ds2.x -=10;
                }
             if(event.key.keysym.sym == SDLK_UP)
                {
                    if((ds2.y) > 0) //limite haut
                        ds2.y -=10;

                }
             if(event.key.keysym.sym == SDLK_DOWN)
                {
                    if((ds2.y + ske_head->w) < (screen->h)) //limite bas
                        ds2.y +=10;
                }


            SDL_BlitSurface(ske_head, 0, screen, &ds2);


                 //boucle de jeu
               }
             SDL_FreeSurface(screen);
            return (0);




            }
             else if(x>=321&&x<=954&&y<=495&&y>=442)
            { choix=2;
            SDL_FreeSurface(game_menu);
                SDL_Surface* screen = SDL_SetVideoMode(1280,720, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
                SDL_Surface* merci =  SDL_LoadBMP("merci.bmp");
                SDL_Rect dstrect3;
    dstrect3.x = (screen->w - merci->w) / 2;
    dstrect3.y = (screen->h - merci->h) / 2;

                  SDL_BlitSurface(merci, 0, screen, &dstrect3);

                    done = true;


            }
            break;
            }


            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = true;
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = true;
                    break;
                }
            } // end switch
        } // end of message processing

        // DRAWING STARTS HERE

        // clear screen
        // SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

        // draw bitmap
       // SDL_BlitSurface(bmp, 0, screen, &dstrect);
        //SDL_BlitSurface(ske_head, 0, screen, &ds2);

        // DRAWING ENDS HERE

        // finally, update the screen :)
        SDL_Flip(screen);
// SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 0));


    } // end main loop

    // free loaded bitmap
    //SDL_FreeSurface(bmp);

    // all is well ;)
    printf("Exited cleanly\n");
    return 0;
}
